<script setup lang="ts" name="WithdrawQrcode">
import {WithdrawQrcodeVo, WithdrawQrcodeForm, WithdrawQrcodeQuery} from '@/api/pay/qrcode/types';
import {
  getWithdrawQrcode,
  updateWithdrawQrcode
} from '@/api/pay/qrcode';

const {proxy} = getCurrentInstance() as ComponentInternalInstance;

const withdrawQrcodeList = ref<WithdrawQrcodeVo[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const withdrawQrcodeFormRef = ref<ElFormInstance>();
const withdrawQrcodeData = ref<WithdrawQrcodeVo>();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: WithdrawQrcodeForm = {
  qrcodeUrl: undefined,
  qrcodeId: undefined
}
const data = reactive<PageData<WithdrawQrcodeForm, WithdrawQrcodeQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    qrcodeUrl: undefined,
    qrcodeId: undefined,
    params: {}
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);


/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  withdrawQrcodeFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: WithdrawQrcodeVo[]) => {
  ids.value = selection.map(item => item.qrcodeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加提现二维码';
};

/** 修改按钮操作 */
const getWithdrawQrcodeData = async () => {
  reset();
  const res = await getWithdrawQrcode();
  Object.assign(form.value, res.data);
};

/** 提交按钮 */
const submitForm = () => {
  withdrawQrcodeFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      await updateWithdrawQrcode(form.value).finally(() => (buttonLoading.value = false));
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getWithdrawQrcode();
    }
  });
};

/** 删除按钮操作 */
// const handleDelete = async (row?: BrokerageConfigVo) => {
//   const _ids = row?.id || ids.value;
//   await proxy?.$modal.confirm('是否确认删除当前选中的数据项？').finally(() => loading.value = false);
//   await delBrokerageConfig(_ids);
//   proxy?.$modal.msgSuccess('删除成功');
//   await getList();
// }

onMounted(() => {
  getWithdrawQrcodeData();
});

</script>

<template>
  <div class="p-2 mt20">
    <!-- 添加或修改分销配置对话框 -->
    <el-form ref="withdrawQrcodeFormRef" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="提现二维码设置" prop="qrcodeUrl">
        <imageUpload single :limit="1" v-model="form.qrcodeUrl" :fileType="['png', 'jpg', 'jpeg']"/>
      </el-form-item>
      <el-form-item>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">保 存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="scss">

</style>
